[main]
filesize=13366790
sha512=06645865a851bfdeca399ed1266f99e072a9bb1d8c6f0d3d6d0f809080ce1e60f45d9e4b52c93cf89f2062a4db0a69f519f23ac5de918bc89bdd794ebd2b96b4
tardir=gcc-3.4.6

[deps]

[mirrors]
http://ftp.gnu.org/gnu/gcc/gcc-3.4.6/gcc-core-3.4.6.tar.bz2

[build]
VERSION=3.4.6
patch -p1 < "$K"/gcc-346-unwind2.patch || exit 1
patch -p1 < "$K"/gcc-346-unlink.patch || exit 1
patch -p1 < "$K"/gcc-346-libgcc-visibility.patch || exit 1

find gcc/config -name linux.h -exec sed -i 's@struct siginfo@siginfo_t@g' "{}" \; || exit 1

if [ "$A" = "i386" ] && [ "$(./config.guess)" = "i686-pc-linux-gnulibc1" ] ; then
	printf '#!/bin/sh\necho i386-unknown-linux-gnu\n' > config.guess
fi


# remove fix-includes
sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in || exit 1

#apparently some control freak gcc used by ubuntu enforces that the mode arg is set in open calls
sed -i 's@O_CREAT@O_CREAT, 0770@' gcc/collect2.c

sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in

sed -i 's@/lib/ld-linux.so.[12]@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux.h
sed -i 's@/lib/ld-linux.so.[12]@/lib/ld-musl-i386.so.1@' gcc/config/i386/linux64.h
sed -i 's@/lib64/ld-linux-x86-64.so.2@/lib/ld-musl-x86_64.so.1@' gcc/config/i386/linux64.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-mips.so.1@' gcc/config/mips/linux.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-mips.so.1@' gcc/config/mips/linux64.h
sed -i 's@/lib64/ld.so.1@/lib/ld-musl-mips.so.1@' gcc/config/mips/linux64.h
sed -i 's@/lib32/ld.so.1@/lib/ld-musl-mips.so.1@' gcc/config/mips/linux64.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-powerpc.so.1@g' gcc/config/rs6000/sysv4.h
sed -i 's@/lib/ld.so.1@/lib/ld-musl-powerpc.so.1@g' gcc/config/rs6000/linux64.h
sed -i 's@/lib64/ld64.so.1@/lib/ld-musl-powerpc64.so.1@g' gcc/config/rs6000/linux64.h


$K/bin/libibertyfix libiberty

archflags=
#[ "$A" = "arm" ] && archflags="--with-float=soft --enable-multilib --enable-interwork"
# gcc 3.4.6 is broken, it emits code for __floatsisf, which according to linaro staff shouldn't be used
# it is supposed to be in libgcc for some archs. should not be too hard to fix, but it yet has to be done
# musl-0.9.2/src/malloc/malloc.c:120: undefined reference to `__floatsisf'

[ "$A" = "arm" ] && archflags="--with-float=soft --with-fpu=vfp --disable-multilib"
[ "$A" = "x86_64" ] && archflags=--disable-multilib
[ "$A" = "i386" ] && archflags=--disable-multilib

# workaround for debian idiocy: they put bits/ into an arch-specific
# sysdir in /usr/include, so the bootstrap compiler (xgcc), which
# uses the host headers, will use a broken set of headers.
is_ubuntu_or_debian() {
	uname -v | grep Ubuntu >/dev/null 2>&1  || \
	uname -v | grep Debian >/dev/null 2>&1
}

if [ "$STAGE" = "0" ] && is_ubuntu_or_debian ; then
	echo debian/ubuntu detected!
	if [ -d /usr/include/"$A"-linux-gnu/bits ] ; then
		for i in sys bits asm gnu ; do
			ln -sf /usr/include/"$A"-linux-gnu/"$i" include/
		done
		# patch for an issue in unwind-dw2.c seen on Ubuntu 13.04: 
		# ./unwind-dw2.c:1032: error: field `info' has incomplete type
		sed -i 's@struct siginfo@siginfo_t@g' gcc/config/i386/linux.h
	fi
fi

./configure --prefix="$butch_prefix" \
  --mandir="$butch_prefix"/share/man \
  --infodir="$butch_prefix"/infoXXX \
  --disable-bootstrap --disable-shared --disable-nls $archflags || exit 1
make \
  CFLAGS="-DSTDC_HEADERS -Werror-implicit-function-declaration" \
  LDFLAGS="-static" \
  -j$MAKE_THREADS || exit 1
make DESTDIR="$butch_install_dir" install-gcc || exit 1

dest="$butch_install_dir""$butch_prefix"

rm -rf "$dest"/infoXXX

# rename gcc to rawcc so musl-gcc can call the *right* gcc
mv "$dest"/bin/gcc "$dest"/bin/rawcc
ln -sf "rawcc" "$dest"/bin/gcc

[ "$A" = "x86_64" ] && mv "$dest"/lib64/* "$dest"lib/ || true

ln -sf rawcc "$dest"/bin/gcc-$VERSION
